/*
 * rwLock.cpp
 *
 *  Created on: 2017年6月15日
 *      Author: orange
 */

#include "rwLock.h"
#include<iostream>
using namespace std;

rwLock::~rwLock() {
	pthread_rwlock_destroy(&rwlock);
}

bool rwLock::readLock()
{
	int nResult = -1;
	nResult = pthread_rwlock_rdlock(&rwlock);
	return nResult == 0;
}

bool rwLock::writeLock()
{
	int nResult = -1;
	nResult = pthread_rwlock_wrlock(&rwlock);
	return nResult == 0;
}

bool rwLock::tryReadLock()
{
	int nResult = -1;
	nResult = pthread_rwlock_tryrdlock(&rwlock);
	return nResult == 0;
}

bool rwLock::tryWriteLock()
{
	int nResult = -1;
	nResult = pthread_rwlock_trywrlock(&rwlock);
	return nResult == 0;
}

bool rwLock::unLock()
{
	int nResult = -1;
	nResult = pthread_rwlock_unlock(&rwlock);
	if(nResult != 0)
	{
		cout<<"unLock 失败!!!请查找原因!"<<endl;
	}
	return nResult == 0;
}
